﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OnlineManagement.IDAL;
using OnlineManagement.DBUtility;
namespace OnlineManagement.SQLServerDAL.Dal
{
	/// <summary>
	/// 数据访问类:Fodder
	/// </summary>
	public partial class Fodder:IFodder
	{
        public Fodder()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int FodderID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Fodder");
            strSql.Append(" where FodderID=@FodderID");
            SqlParameter[] parameters = {
					new SqlParameter("@FodderID", SqlDbType.Int,4)
			};
            parameters[0].Value = FodderID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(OnlineManagement.Model.Fodder model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Fodder(");
            strSql.Append("HoggeryID,PhaseID,StateID,FodderName,FodderFactory,ProviderName,FodderConstitution,FodderDecription)");
            strSql.Append(" values (");
            strSql.Append("@HoggeryID,@PhaseID,@StateID,@FodderName,@FodderFactory,@ProviderName,@FodderConstitution,@FodderDecription)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@HoggeryID", SqlDbType.Int,4),
					new SqlParameter("@PhaseID", SqlDbType.Int,4),
					new SqlParameter("@StateID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@FodderName", SqlDbType.NVarChar,50),
					new SqlParameter("@FodderFactory", SqlDbType.NVarChar,50),
					new SqlParameter("@ProviderName", SqlDbType.NVarChar,50),
					new SqlParameter("@FodderConstitution", SqlDbType.Text),
					new SqlParameter("@FodderDecription", SqlDbType.NVarChar,500)};
            parameters[0].Value = model.HoggeryID;
            parameters[1].Value = model.PhaseID;
            parameters[2].Value = Guid.NewGuid();
            parameters[3].Value = model.FodderName;
            parameters[4].Value = model.FodderFactory;
            parameters[5].Value = model.ProviderName;
            parameters[6].Value = model.FodderConstitution;
            parameters[7].Value = model.FodderDecription;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(OnlineManagement.Model.Fodder model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Fodder set ");
            strSql.Append("HoggeryID=@HoggeryID,");
            strSql.Append("PhaseID=@PhaseID,");
            strSql.Append("StateID=@StateID,");
            strSql.Append("FodderName=@FodderName,");
            strSql.Append("FodderFactory=@FodderFactory,");
            strSql.Append("ProviderName=@ProviderName,");
            strSql.Append("FodderConstitution=@FodderConstitution,");
            strSql.Append("FodderDecription=@FodderDecription");
            strSql.Append(" where FodderID=@FodderID");
            SqlParameter[] parameters = {
					new SqlParameter("@HoggeryID", SqlDbType.Int,4),
					new SqlParameter("@PhaseID", SqlDbType.Int,4),
					new SqlParameter("@StateID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@FodderName", SqlDbType.NVarChar,50),
					new SqlParameter("@FodderFactory", SqlDbType.NVarChar,50),
					new SqlParameter("@ProviderName", SqlDbType.NVarChar,50),
					new SqlParameter("@FodderConstitution", SqlDbType.Text),
					new SqlParameter("@FodderDecription", SqlDbType.NVarChar,500),
					new SqlParameter("@FodderID", SqlDbType.Int,4)};
            parameters[0].Value = model.HoggeryID;
            parameters[1].Value = model.PhaseID;
            parameters[2].Value = model.StateID;
            parameters[3].Value = model.FodderName;
            parameters[4].Value = model.FodderFactory;
            parameters[5].Value = model.ProviderName;
            parameters[6].Value = model.FodderConstitution;
            parameters[7].Value = model.FodderDecription;
            parameters[8].Value = model.FodderID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int FodderID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Fodder ");
            strSql.Append(" where FodderID=@FodderID");
            SqlParameter[] parameters = {
					new SqlParameter("@FodderID", SqlDbType.Int,4)
			};
            parameters[0].Value = FodderID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string FodderIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Fodder ");
            strSql.Append(" where FodderID in (" + FodderIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OnlineManagement.Model.Fodder GetModel(int FodderID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FodderID,HoggeryID,PhaseID,StateID,FodderName,FodderFactory,ProviderName,FodderConstitution,FodderDecription from Fodder ");
            strSql.Append(" where FodderID=@FodderID");
            SqlParameter[] parameters = {
					new SqlParameter("@FodderID", SqlDbType.Int,4)
			};
            parameters[0].Value = FodderID;

            OnlineManagement.Model.Fodder model = new OnlineManagement.Model.Fodder();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public OnlineManagement.Model.Fodder DataRowToModel(DataRow row)
        {
            OnlineManagement.Model.Fodder model = new OnlineManagement.Model.Fodder();
            if (row != null)
            {
                if (row["FodderID"] != null && row["FodderID"].ToString() != "")
                {
                    model.FodderID = int.Parse(row["FodderID"].ToString());
                }
                if (row["HoggeryID"] != null && row["HoggeryID"].ToString() != "")
                {
                    model.HoggeryID = int.Parse(row["HoggeryID"].ToString());
                }
                if (row["PhaseID"] != null && row["PhaseID"].ToString() != "")
                {
                    model.PhaseID = int.Parse(row["PhaseID"].ToString());
                }
                if (row["StateID"] != null && row["StateID"].ToString() != "")
                {
                    model.StateID = new Guid(row["StateID"].ToString());
                }
                if (row["FodderName"] != null)
                {
                    model.FodderName = row["FodderName"].ToString();
                }
                if (row["FodderFactory"] != null)
                {
                    model.FodderFactory = row["FodderFactory"].ToString();
                }
                if (row["ProviderName"] != null)
                {
                    model.ProviderName = row["ProviderName"].ToString();
                }
                if (row["FodderConstitution"] != null)
                {
                    model.FodderConstitution = row["FodderConstitution"].ToString();
                }
                if (row["FodderDecription"] != null)
                {
                    model.FodderDecription = row["FodderDecription"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FodderID,HoggeryID,PhaseID,StateID,FodderName,FodderFactory,ProviderName,FodderConstitution,FodderDecription ");
            strSql.Append(" FROM Fodder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FodderID,HoggeryID,PhaseID,StateID,FodderName,FodderFactory,ProviderName,FodderConstitution,FodderDecription ");
            strSql.Append(" FROM Fodder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Fodder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FodderID desc");
            }
            strSql.Append(")AS Row, T.*  from Fodder T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
	}
}

